package com.bfly.mall.order.api;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.Login;
import com.bfly.mall.order.dto.BookingOrderDTO;
import com.bfly.mall.order.entity.OrderCart;
import com.bfly.mall.order.dto.OrderCartDTO;
import com.bfly.mall.order.dto.OrderCartListDTO;
import com.bfly.mall.order.service.IOrderCartService;
import com.bfly.mall.order.service.IOrderService;
import com.bfly.mall.user.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车API Controller
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/4/19 9:23
 */
@RestController
@RequestMapping(value = "/api/cart")
public class OrderCartApiController extends BaseController {

    @Autowired
    private IOrderCartService cartService;
    @Autowired
    private IOrderService orderService;

    /**
     * 购物车列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/19 9:48
     */
    @GetMapping(value = "/list")
    @Login
    public void listOrderCart(HttpServletResponse response) {
        List<OrderCart> list = cartService.getOrderCart(getUser().getId());
        List<OrderCartListDTO> carts = new ArrayList<>();
        if (list != null) {
            list.forEach(cart -> {
                OrderCartListDTO dto = new OrderCartListDTO();
                BeanUtils.copyProperties(cart, dto);
                carts.add(dto);
            });
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(carts));
    }


    /**
     * 删除购物车
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/25 13:17
     */
    @GetMapping(value = "/remove")
    @ActionModel(value = "删除购物车")
    @Login
    public void removeOrderCart(@RequestParam("productId") int productId, @RequestParam("sn") String sn, HttpServletResponse response) {
        cartService.remove(getUser().getId(), productId, sn);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 添加到购物车
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/4/25 14:18
     */
    @PostMapping(value = "/add")
    @ActionModel(value = "添加到购物车")
    @Login
    public void addOrderCart(@RequestBody OrderCartDTO cartDTO, HttpServletResponse response) {
        cartService.addToCart(cartDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 返回购物车中产品数量
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/15 12:14
     */
    @GetMapping(value = "/count")
    public void getProductCountFromCart(HttpServletResponse response) {
        User user = getUser();
        int productCount = 0;
        if (user != null) {
            productCount = cartService.getProductCount(user.getId());
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(productCount));
    }

    /**
     * 计算产品的运费
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/16 22:16
     */
    @PostMapping(value = "/calculate/freight")
    @Login
    public void calculateOrderFreight(HttpServletResponse response, @RequestBody BookingOrderDTO bookingOrder) {
        BigDecimal bigDecimal = orderService.calculateOrderFreight(getUser().getId(), bookingOrder);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(bigDecimal));
    }

    /**
     * 校验优惠卷是否可用本次订单
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/18 16:01
     */
    @PostMapping(value = "/calculate/discount")
    @Login
    public void calculateOrderCouponDiscount(HttpServletResponse response, @RequestBody BookingOrderDTO bookingOrder) {
        BigDecimal bigDecimal = orderService.calculateOrderDiscount(getUser().getId(), bookingOrder);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(bigDecimal));
    }
}
